#include "mygraphicsview.h"
#include <QDebug>
#include <QWheelEvent>

MyGraphicsView::MyGraphicsView(QWidget *parent)
{
    this->setParent(parent);
    this->setDragMode(QGraphicsView::NoDrag);
    this->setMouseTracking(true);
    this->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//隐藏水平条
    this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//隐藏竖条
    this->setStyleSheet("padding: 0px; border: 0px;"); //无边框
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    this->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
}

void MyGraphicsView::wheelEvent ( QWheelEvent * event )
{
    if(event->modifiers() == Qt::CTRL)
    {
        if((event->delta()>0) && scale_m>50)
        {
            return;
        }
        else if((event->delta() < 0)&&(scale_m <= 0.01))//图像缩小到自适应大小之后就不继续缩小
         {
             return;//重置图片大小和位置，使之自适应控件窗口大小
         }
         else
         {
             // 当前放缩倍数;
             qreal scaleFactor = this->matrix().m11();
             scale_m = scaleFactor;

             int wheelDeltaValue = event->delta();
             // 向上滚动，放大;
             if (wheelDeltaValue > 0)
             {
                 this->scale(1.2, 1.2);
             }
             else
             {// 向下滚动，缩小;
                 this->scale(1.0 / 1.2, 1.0 / 1.2);
             }
             update();
         }
     }
}

void MyGraphicsView::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        qDebug()<<"leftbutton";
       // qDebug()<<this->pos();
    }
}
